package com.kcwx.web.controller.system;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.kcwx.common.annotation.Log;
import com.kcwx.common.constant.UserConstants;
import com.kcwx.common.core.controller.BaseController;
import com.kcwx.common.core.domain.AjaxResult;
import com.kcwx.common.core.page.TableDataInfo;
import com.kcwx.common.enums.BusinessType;
import com.kcwx.common.utils.poi.ExcelUtil;

import com.kcwx.shareddomain.common.dto.DeleteDto;
import com.kcwx.shareddomain.common.dto.DelParaValid;
import com.kcwx.shareddomain.system.query.SysPostQuery;
import com.kcwx.shareddomain.system.dto.SysPostEditDto;

import com.kcwx.system.dtomap.SysPostMap;
import com.kcwx.system.domain.SysPost;
import com.kcwx.system.service.ISysPostService;

/**
 * 岗位信息操作处理
 * 
 */
@Api(tags = "系统管理-岗位管理")
@RestController
@RequestMapping("/system/post")
public class SysPostController extends BaseController
{
    @Autowired
    private ISysPostService postService;
    @Autowired
    private SysPostMap sysPostMap;

    /**
     * 新增岗位
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('system:post:add')")
    @Log(title = "新增岗位", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody SysPostEditDto dto)
    {
        SysPost post = sysPostMap.AddDtoToEntity(dto);
        if (UserConstants.NOT_UNIQUE.equals(postService.checkPostNameUnique(post)))
        {
            return error("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post)))
        {
            return error("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setCreateBy(getUsername());
        int rowsAffect=postService.insertPost(post);
        return rowsAffect>0?success():error("新增失败");
    }

    /**
     * 修改岗位
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('system:post:edit')")
    @Log(title = "修改岗位", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody SysPostEditDto dto)
    {
        SysPost post = sysPostMap.EditDtoToEntity(dto);
        if (UserConstants.NOT_UNIQUE.equals(postService.checkPostNameUnique(post)))
        {
            return error("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post)))
        {
            return error("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setUpdateBy(getUsername());
        int rowsAffect=postService.updatePost(post);
        return rowsAffect>0?success():error("修改失败");
    }

    /**
     * 删除岗位
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('system:post:remove')")
    @Log(title = "删除岗位", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove")
    public AjaxResult remove(@RequestBody DeleteDto dto)
    {
        Long id=DelParaValid.validDel(dto);
        int rowsAffect=postService.deletePostById(id,getUsername());
        return rowsAffect>0?success():error("删除失败");
    }

    /**
     * 批量删除岗位
     */
    @ApiOperation("批量删除")
    @PreAuthorize("@ss.hasPermi('system:post:remove')")
    @Log(title = "批量删除岗位", businessType = BusinessType.DELETE)
    @DeleteMapping("/batchRemove")
    public AjaxResult batchRemove(@RequestBody DeleteDto dto)
    {
        Long []ids=DelParaValid.validBatchDel(dto);
        int rowsAffect=postService.deletePostByIds(ids,getUsername());
        return rowsAffect>0?success():error("删除失败");
    }
    /**
     * 分页查询
     */
    @ApiOperation("获取岗位列表")
    @PreAuthorize("@ss.hasPermi('system:post:list')")
    @Log(title = "岗位分页查询", businessType = BusinessType.OTHER)
    @PostMapping("/list")
    public AjaxResult list(@RequestBody SysPostQuery post)
    {
        startPage();
        List<SysPost> list = postService.selectPostList(post);
        return success(getDataTable(list));
    }

    /**
     * 根据id获取详细信息
     */
    @ApiOperation("获取详细信息")
    @PreAuthorize("@ss.hasPermi('system:post:query')")
    @Log(title = "根据id获取岗位", businessType = BusinessType.OTHER)
    @GetMapping( "/getById")
    public AjaxResult getInfo(Long id)
    {
        return success(postService.selectPostById(id));
    }

    /**
     * 获取岗位选择框列表
     */
    @ApiOperation("获取岗位选择框列表")
    @Log(title = "获取岗位选择框列表", businessType = BusinessType.OTHER)
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<SysPost> posts = postService.selectPostAll();
        if(!posts.isEmpty()){
            posts=posts.stream().filter(postItem->postItem.getStatus()==1).collect(Collectors.toList());
        }
        return success(posts);
    }
}
